from tornado import gen
from tornado.concurrent import Future
from tornado.httpclient import HTTPClient, AsyncHTTPClient

def synchronous_fetch(url):
    # a sample synchronous function
    http_client = HTTPClient()
    response = http_client.fetch(url)
    return response.body

def asynchronous_fetch(url, callback):
    # asynchronous with a callback argument
    http_client = AsyncHTTPClient()
    def handle_response(response):
        callback(response.body)
    http_client.fetch(url, callback=handle_response)

def async_fetch_future(url):
    #  with a Future instead of a callback:
    http_client = AsyncHTTPClient()
    my_future = Future()
    fetch_future = http_client.fetch(url)
    fetch_future.add_done_callback(
        lambda f: my_future.set_result(f.result()))
    return my_future

@gen.coroutine
def fetch_coroutine(url):
    http_client = AsyncHTTPClient()
    response = yield http_client.fetch(url)
    raise gen.Return(response.body)